package com.kgc.server.controller;

import com.kgc.server.pojo.EasybuyProduct;
import com.kgc.server.pojo.Easybuy_product_category;
import com.kgc.server.pojo.Page;
import com.kgc.server.service.EasybuyProductCategoryService;
import com.kgc.server.service.ProductService;
import com.kgc.server.util.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
public class ProductController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ProductService productService;
    @Autowired
    private EasybuyProductCategoryService easybuyProductCategoryService;

    @RequestMapping(value="getProductList",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    @CrossOrigin
    public List getProductList(Page page, String name){
        logger.info("ProductController getProductList page:"+page+",name:"+name);
        page.setName(name);
        List list = productService.getProductList(page);
        list.add(page);
        logger.info("ProductController getProductList page:"+page+",name:"+name+",list:"+list);
        return list;
    }

    @RequestMapping("test")
    @ResponseBody
    @CrossOrigin
    public ResponseMessage getProductById(String testId) {
        logger.info("ProductController getProductById testId:"+testId);
        ResponseMessage productById = productService.getProductById(testId);
        logger.info("ProductController getProductById testId:"+testId+",productById:"+productById);
        return productById;
    }

    @RequestMapping("add")
    @ResponseBody
    public ResponseMessage addProductToSolr() {
        logger.info("ProductController addProductToSolr start");
        ResponseMessage responseMessage = productService.addProductToSole();
        logger.info("ProductController addProductToSolr responseMessage:"+responseMessage);
        return responseMessage;
    }

    @RequestMapping("delProductListById")
    @ResponseBody
    @CrossOrigin
    public boolean delProductListById(int id){
        logger.info("ProductController delProductListById id:"+id);
        int num = productService.delProductListById(id);
        logger.info("ProductController delProductListById id:"+id+",num:"+num);
        if( num > 0 ){
            return true;
        }else{
            return false;
        }
    }

    @RequestMapping("getAllProduct")
    @ResponseBody
    @CrossOrigin
    public ResponseMessage getAllProduct() {
        logger.info("ProductController getAllProduct start...");
        ResponseMessage msg = productService.getAllProduct();
        logger.info("ProductController getAllProduct msg:"+msg);
        return msg;
    }

    @RequestMapping("getProductListById")
    @ResponseBody
    @CrossOrigin
    public List getProductListById(int id){
        logger.info("ProductController getProductListById id:"+id);
        List list = productService.getProductListById(id);
        logger.info("ProductController getProductListById id:"+id+",list:"+list);
        return list;
    }

    @RequestMapping("addProduct")
    @ResponseBody
    @CrossOrigin
    public ResponseMessage addProduct(EasybuyProduct easybuyProduct, @RequestParam(value="a_picPath") MultipartFile a_picPath) {
        logger.info("ProductController addProduct params:" + easybuyProduct + ";a_picPath:" + a_picPath);
        String fileName1 = null;
        String fileName2 = null;
        if (!a_picPath.isEmpty()) {
            String picPath = a_picPath.getOriginalFilename();
            logger.info("图片名字："+picPath);
            String fileHZ =picPath.substring(picPath.lastIndexOf("."));
            logger.info("图片后缀："+fileHZ);
            String fileHeader = UUID.randomUUID().toString();
            //D:\esaybuy\ymw-index\client\files
            fileName1 = "D:/esaybuy/ymw-index/client/files/"+fileHeader+fileHZ;
            fileName2=fileHeader+fileHZ;
            logger.info("存到数据库的路径："+fileName2);
            logger.info("文件路径："+fileName1);
            File file = new File(fileName1);
            try {
                a_picPath.transferTo(file);
            } catch (IOException e) {
                logger.error("ProductController addProduct error params:" + easybuyProduct + ";a_picPath:" + a_picPath,e);
                e.printStackTrace();
            }
        }
        easybuyProduct.setFileName(fileName2);
        ResponseMessage responseMessage = productService.addProduct(easybuyProduct);
        logger.info("返回给前端的集合："+responseMessage);
        return responseMessage;
    }

    @RequestMapping("/updateProductToSql")
    @ResponseBody
    @CrossOrigin
    public boolean updateProductToSql(EasybuyProduct product, @RequestParam(value = "a_picPath") MultipartFile a_picPath){
        logger.info("ProductController updateProductToSql param product:"+product);
        String fileName = null;
        String fileName2 = null;
        if(!a_picPath.isEmpty()) {
            String oldFileName = a_picPath.getOriginalFilename();
            String fileHZ = oldFileName.substring(oldFileName.lastIndexOf("."));
            logger.info("文件后缀名:"+fileHZ);
            String fileZH = UUID.randomUUID().toString();
            fileName = "D:/esaybuy/ymw-index/client/files/"+fileZH+fileHZ;
            fileName2 = fileZH+fileHZ;
            logger.info("文件路径:"+fileName);
            File file = new File(fileName);
            try {
                a_picPath.transferTo(file);
            } catch (Exception e) {
                logger.error("ProductController updateProductToSql isError",e);
            }
        }
        product.setFileName(fileName2);
        int num = productService.updataProductListById(product);
        if( num > 0 ){
            return true;
        }
        return false;
    }

    @RequestMapping("/updateProductToSql1")
    @ResponseBody
    @CrossOrigin
    public boolean updateProductToSql1(EasybuyProduct product){
        logger.info("updateProductToSql1 product:"+product);
        int num = productService.updataProductListById(product);
        logger.info("updateProductToSql1 product:"+product+",num:"+num);
        if( num > 0 ){
            return true;
        }
        return false;
    }

    @RequestMapping("/setGoodsCollection")
    @ResponseBody
    @CrossOrigin
    public boolean setGoodsCollection(int userID,int goodsID){
        logger.info("setGoodsCollection userID:"+userID+",goodsID:"+goodsID);
        boolean flag = productService.setGoodsCollection(goodsID,userID);
        logger.info("setGoodsCollection userID:"+userID+",goodsID:"+goodsID+",flag:"+flag);
        if( flag == true ){
            return true;
        }
        return false;
    }

    @RequestMapping("/delGoodsCollection")
    @ResponseBody
    @CrossOrigin
    public boolean delGoodsCollection(int goodsID,int userID){
        logger.info("delGoodsCollection userID:"+userID+",goodsID:"+goodsID);
        boolean flag = productService.delGoodsCollection(goodsID,userID);
        logger.info("delGoodsCollection userID:"+userID+",goodsID:"+goodsID+",flag:"+flag);
        if( flag == true ){
            return true;
        }
        return false;
    }

    @RequestMapping("getProductForSolr")
    @ResponseBody
    @CrossOrigin
    public ResponseMessage getProductForSolr(Integer categoryLevel1Id, Integer categoryLevel3Id, String name, Integer pageNo){
        logger.info("ProductController getProductForSolr params: categoryLevel1Id :"+categoryLevel1Id+";categoryLevel3Id:"+categoryLevel3Id+";name:"+name+";pageNo:"+pageNo);
        ResponseMessage msg = productService.getProductForSolr(categoryLevel1Id, categoryLevel3Id, name, pageNo);
        logger.info("ProductController getProductForSolr params: categoryLevel1Id :"+categoryLevel1Id+";categoryLevel3Id:"+categoryLevel3Id+";name:"+name+";pageNo:"+pageNo+",msg:"+msg);
        return msg;
    }

    @RequestMapping("stockIsEnough")
    @ResponseBody
    @CrossOrigin
    public ResponseMessage stockIsEnough(String userId){
        logger.info("ProductController stockIsEnough param userId:"+userId);
        ResponseMessage responseMessage = productService.stockIsEnough(userId);
        return responseMessage;
    }
    @RequestMapping("ifIsflagCollection")
    @ResponseBody
    @CrossOrigin
    public boolean ifIsflagCollection(int userID,int goodsID){
        logger.info("ProductController ifIsflagCollection param userId:"+userID+",goodsID:"+goodsID);
        boolean flag = productService.getCollection(goodsID,userID);
        return flag;
    }

    @RequestMapping("getUserProductListByUserId")
    @ResponseBody
    @CrossOrigin
    public List getUserProductListByUserId(int userID){
        logger.info("ProductController getUserProductListByUserId param userID:"+userID);
        List list = productService.getUserProductListByUserId(userID);
        return list;
    }

    @RequestMapping("getCollectionFy")
    @ResponseBody
    @CrossOrigin
    public List getCollectionFy(Page page,int userID){
        logger.info("ProductController getCollectionFy param userID:"+userID+",page:"+page);
        List list = productService.getCollectionFy(page,userID);
        list.add(page);
        logger.info("ProductController getCollectionFy param userID:"+userID+",page:"+page+"list:"+list);
        return list;
    }

    @RequestMapping("delCollectionByUserIdORID")
    @ResponseBody
    @CrossOrigin
    public boolean delCollectionByUserIdORID(int userID,int goodsID){
        logger.info("ProductController delCollectionByUserIdORID param userID:"+userID+",goodsID:"+goodsID);
        int num = productService.delCollectionByUserIdORID(userID,goodsID);
        logger.info("ProductController delCollectionByUserIdORID param userID:"+userID+",goodsID:"+goodsID+",num:"+num);
        if( num > 0 ){
            return true;
        }
        return false;
    }

    @RequestMapping("getCollectionAll")
    @ResponseBody
    @CrossOrigin
    public List getCollectionAll(int userID){
        logger.info("ProductController getCollectionAll param userID:"+userID);
        List list = productService.getCollectionAll(userID);
        return list;
    }
}
